﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web.UI;
using Sharp.Infrastructure;

namespace Sharp.Platform.Configuration
{
    public class ConfigManage
    {
        static ConfigManage()
        {
            ConfigurationGroupList = new List<ConfigurationGroup>();
        }

        /// <summary>
        /// 配置项组列表
        /// </summary>
        public static List<ConfigurationGroup> ConfigurationGroupList
        {
            get;
            private set;
        }

        private static ConfigurationCatalog FindConfigurationCatalogFromGroup(Type cfg, ConfigurationGroup @group, String catalog)
        {
            foreach (ConfigurationCatalog oCatalog in @group.Items)
            {
                if (oCatalog.Title == catalog)
                {
                    return oCatalog;
                }
            }
            var oItem = new ConfigurationCatalog(catalog)
            {
                ConfigType = cfg
            };
            @group.Items.Add(oItem);
            return oItem;
        }
        /// <summary>
        /// 注册配置项
        /// </summary>
        /// <param name="cfg">配置文件</param>
        /// <param name="group">组</param>
        /// <param name="catalog">目录</param>
        /// <param name="item">配置项对象</param>
        /// <param name="frame"></param>
        private static void RegisterConfiguration(Type cfg, string @group, string catalog, ConfigurationItem item, string frame = "")
        {
            //重复的不增加
            if (ConfigurationGroupList.Any(x =>
                x.Caption == @group
                && x.Items.Any(y => y.Title == catalog && y.Items.Any(z => item != null && z.Title == item.Title)
                )))
            {
                return;
            }

            ConfigurationCatalog oCatalog = (from oGroup in ConfigurationGroupList
                                             where oGroup.Caption == @group
                                             select FindConfigurationCatalogFromGroup(cfg, oGroup, catalog)).FirstOrDefault();

            if (oCatalog == null)
            {
                var oNewGroup = new ConfigurationGroup(@group);
                ConfigurationGroupList.Add(oNewGroup);
                oCatalog = FindConfigurationCatalogFromGroup(cfg, oNewGroup, catalog);
            }
            if (frame.Length == 0)
            {
                oCatalog.Items.Add(item);
            }
            else
            {
                //oCatalog.IFrame = frame;
            }
        }
 
 

        #region 获取用户级配置清单
        /// <summary>
        /// 获取用户级配置清单
        /// </summary>
        public static List<Type> UserConfigs()
        {
            List<Type> list = new List<Type>();
            string bin = WebServer.MapPath("~/bin/");
            foreach (var dll in new DirectoryInfo(bin).GetFiles("*.dll"))
            {
                if (dll.Exists)
                {
                    Assembly ass = Assembly.LoadFrom(dll.FullName);
                    var ts = ass.GetTypes().Where(x => x.GetInterface("IUserConfig") != null).ToArray();

                    list.AddRange(ts);
                }
            }

            return list;
        }


        /// <summary>
        /// 获取用户级配置清单
        /// </summary>
        /// <param name="typeFullName"></param>
        /// <returns></returns>
        public static Type UserConfigs(string typeFullName)
        {
            return UserConfigs().FirstOrDefault(x => x.FullName == typeFullName);
        }
        #endregion

        #region 获取用户级配置清单
        /// <summary>
        /// 获取用户级配置清单
        /// </summary>
        public static List<Type> SystemConfigs()
        {
            List<Type> list = new List<Type>();
            string bin = WebServer.MapPath("~/bin/");
            foreach (var dll in new DirectoryInfo(bin).GetFiles("*.dll"))
            {
                if (dll.Exists)
                {
                    try
                    {
                        Assembly ass = Assembly.LoadFrom(dll.FullName);
                        var ts = ass.GetTypes().Where(x => x.GetInterface("ISystemConfig") != null).ToArray();

                        list.AddRange(ts);
                    }
                    catch { }
                  
                }
            }

            return list;
        }


        /// <summary>
        /// 获取用户级配置清单
        /// </summary>
        /// <param name="typeFullName"></param>
        /// <returns></returns>
        public static Type SystemConfigs(string typeFullName)
        {
            return SystemConfigs().FirstOrDefault(x => x.FullName == typeFullName);
        }
        #endregion

        #region 初始化配置界面的实体类
        /// <summary>
        /// 初始化配置界面的实体类
        /// </summary>
        public static void InitUI()
        {
            foreach (var cfg in ConfigManage.UserConfigs())
            {
                var pName = cfg.GetProperties();
                foreach (var p in pName)
                {
                    var display = p.GetCustomAttributes(typeof(DisplayNameAttribute), false).FirstOrDefault() as DisplayNameAttribute;
                    var Category = p.GetCustomAttributes(typeof(CategoryAttribute), false).FirstOrDefault() as CategoryAttribute;

                    var Js = p.GetCustomAttributes(typeof(JavascriptAttribute), false).FirstOrDefault() as JavascriptAttribute;
                    var url = p.GetCustomAttributes(typeof(UrlPropertyAttribute), false).FirstOrDefault() as UrlPropertyAttribute;


                    if (display == null || Category == null)
                    {
                        continue;
                    }

                    string group = Category.Category.Split(new[] { ">" }, StringSplitOptions.RemoveEmptyEntries)[0];
                    string category = Category.Category.Split(new[] { ">" }, StringSplitOptions.RemoveEmptyEntries)[1];
                    string script = string.Empty;
                    if (Js != null)
                    {
                        script = Js.Script;
                    }
                    string strUrl = string.Empty;
                    if (url != null)
                    {
                        strUrl = url.Filter;
                    }

                    if (!strUrl.IsNullOrEmpty())
                    {
                        //RegisterConfigurationIFrame(group, category, strUrl);
                    }
                    else
                    {
                        //RegisterConfiguration(cfg, group, category, display.DisplayName, p.Name, script);
                    }
                }
            }
        }
        #endregion

    }
}
